package com.shushan.db;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.shushan.login.bean.ChildBean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName: ChildBeanDao
 * @Desciption: 定义ChildBean数据访问对象，对指定的表进行增删改查操作
 * @author: yichaohua
 * @date: 2018-02-08
 */
public class ChildBeanDao {

    private static final String TAG = "ChildBeanDao";

    private Dao<ChildBean, Integer> childBeanDao;
    private DBHelper dbHelper;

    /**
     * 构造方法
     * 获得数据库帮助类实例，通过传入Class对象得到相应的Dao
     * @param context
     */
    public ChildBeanDao(Context context) {
        try {
            dbHelper = DBHelper.getHelper(context);
            childBeanDao = dbHelper.getDao(ChildBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if(childBeanDao == null){
            Log.e(TAG,"childBeanDao == null");
        }
    }

    /**
     * 添加一条记录
     * @param childBean
     */
    public void add(ChildBean childBean) {
        try {
            childBeanDao.create(childBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除一条记录
     * @param childBean
     */
    public void delete(ChildBean childBean) {
        try {
            childBeanDao.delete(childBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新一条记录
     * @param childBean
     */
    public void update(ChildBean childBean) {
        try {
            childBeanDao.update(childBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void updateStarts(int id,int stars){
        if(id <= 0 || stars <= 0){
            return;
        }

        ChildBean childBean = queryForId(id);
        if(childBean != null){
            childBean.stars += stars;
        }

        update(childBean);
    }

    /**
     * 查询一条记录
     * @param id
     * @return
     */
    public ChildBean queryForId(int id) {
        ChildBean childBean = null;
        try {
            childBean = childBeanDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return childBean;
    }

    /**
     * 查询所有记录
     * @return
     */
    public List<ChildBean> queryForAll() {
        List<ChildBean> childBeanList = new ArrayList<ChildBean>();
        try {
            childBeanList = childBeanDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return childBeanList;
    }

    public boolean contain(int id){
        List<ChildBean> childBeanList = queryForAll();
        for (ChildBean childBean : childBeanList){
            if(childBean.id == id){
                return true;
            }
        }

        return false;
    }

    public void clearTab(){
        try {
            childBeanDao.queryRaw("delete from children_table");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public void addOrUpdateList(List<ChildBean> chilidList){
        if(chilidList == null){
            return;
        }

        for (ChildBean childBean : chilidList){
            if(contain(childBean.id)){
                update(childBean);
            }else {
                add(childBean);
            }
        }
    }

    public void dropAndAddList(List<ChildBean> chilidList){
        if(chilidList == null || chilidList.size() == 0){
            return;
        }

        clearTab();

        addOrUpdateList(chilidList);

    }
}
